package com.collect.senegal.zhiHelper;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

import com.collect.senegal.MyApplication;
import com.collect.senegal.zhiBean.zhiReceive.ZHIRAppVersionEntity;
import com.collect.senegal.zhiGlobal.zhiAppSettings.ZHIProjectConfigs;
import com.collect.senegal.zhiUtils.zhiVersion.ZHIVersionIMPL;

public class ZHIAppVersionHelper {

    private static String TAG = "CFAppVersionHelper";

    public static ZHIAppVersionHelper getInstance() {
        return instance;
    }

    private static ZHIAppVersionHelper instance = new ZHIAppVersionHelper();

    /**新版本号*/
    private String gpVersion;

    public String getGpVersion() {
        return gpVersion;
    }

    public void setGpVersion(String gpVersion) {
        this.gpVersion = gpVersion;
    }

    /**
     * 处理app更新，如果不需要更新，返回NO，其它返回YES
     *
     * @type method
     * @author liwenjie
     * @creatTime 2019-11-15 14:46
     */
    public boolean handlerAppVersion(Context context, ZHIRAppVersionEntity zhirAppVersionEntity) {

        return ZHIVersionIMPL.handlerAppVersion(context, zhirAppVersionEntity);

//        if (TextUtils.isEmpty(zhirAppVersionEntity.getLinkUrl())) {
//            return false;
//        }
//
//        int oriInt = Integer.parseInt(ZHIProjectConstants.APP_VERSION_FLAG);
//
//        String gpVersion = zhirAppVersionEntity.getGpVersion();
//        if (TextUtils.isEmpty(gpVersion)) {
//            return false;
//        }
//        setGpVersion(gpVersion);
//        int gpInt = Integer.parseInt(gpVersion);
//
//        if (gpInt > oriInt) {
//            ApkInfo apkInfo = new ApkInfo(9999, context.getResources().getString(R.string.app_name), zhirAppVersionEntity.getLinkUrl());
//            new ApkDownloader(context, apkInfo).downloadApk();
//            return true;
//        }
    }

    /**
     * 判断是否是过审版本
     *
     * @type method
     * @author liwenjie
     * @creatTime 2019-12-25 16:44
     */
    public boolean isPushVersion(String serVersion) {
        if (TextUtils.isEmpty(serVersion)) {
            return true;
        }

        int serInt = Integer.parseInt(serVersion);
        int oriInt = Integer.parseInt(ZHIProjectConfigs.APP_VERSION);

        if (serInt < oriInt) {
            return false;
        }

        return true;
    }

    /**
     * 判断是否是debug版本
     *
     * @type method
     * @author liwenjie
     * @creatTime 2020-01-13 17:33
     */
    public static boolean isDebug(Context context) {
        try {
            ApplicationInfo info = context.getApplicationInfo();
            return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
        } catch (Exception e) {
            return false;
        }
    }


    /**
     * 打开google play应用商店
     *
     * @type method
     * @author liwenjie
     * @creatTime 2020-01-13 17:33
     */
    public static void openGooglePlayStore(String url) {
        if (TextUtils.isEmpty(url)) {
            Log.e(TAG, "GoogleMarket: URL not found");
            return;
        }
        try {
            //这里对应的是谷歌商店，跳转别的商店改成对应的即可
            final String GOOGLE_PLAY = "com.android.vending";

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            intent.setPackage(GOOGLE_PLAY);
            //这里对应的是谷歌商店，跳转别的商店改成对应的即可
            if (intent.resolveActivity(MyApplication.getAppContext().getPackageManager()) != null) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                MyApplication.getAppContext().startActivity(intent);
            } else {
                //没有应用市场，通过浏览器跳转到Google Play
                Intent intent2 = new Intent(Intent.ACTION_VIEW);
                intent2.setData(Uri.parse(ZHIProjectConfigs.APP_GOOGLE_PLAY_URL));
                if (intent2.resolveActivity(MyApplication.getAppContext().getPackageManager()) != null) {
                    intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApplication.getAppContext().startActivity(intent2);
                } else {
                    //没有Google Play 也没有浏览器
                    Log.e(TAG, "GoogleMarket Intent not found");
                }
            }
        } catch (ActivityNotFoundException activityNotFoundException1) {
            Log.e(TAG, "GoogleMarket Intent not found");
        }
    }
}
